package com.example.gulimallmember.service.impl;

import com.example.gulimallmember.dao.MemberLevelDao;
import com.example.gulimallmember.entity.MemberLevelEntity;
import com.example.gulimallmember.exception.PhoneExistException;
import com.example.gulimallmember.exception.UsernameExistException;
import com.example.gulimallmember.service.MemberLevelService;
import com.example.gulimallmember.vo.GiteeUserVo;
import com.example.gulimallmember.vo.MemberLoginVo;
import com.example.gulimallmember.vo.MemberRegistVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.utils.PageUtils;
import com.example.common.utils.Query;

import com.example.gulimallmember.dao.MemberDao;
import com.example.gulimallmember.entity.MemberEntity;
import com.example.gulimallmember.service.MemberService;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {
    @Autowired
    MemberLevelDao memberLevelDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void regist(MemberRegistVo vo) {
        checkPhoneUnique(vo.getPhone());
        checkUsernameUnique(vo.getUserName());
        MemberEntity memberEntity = new MemberEntity();
        //查默认等级
        MemberLevelEntity levelEntity = memberLevelDao.getDefaultLevel();
        memberEntity.setLevelId(levelEntity.getId());
        memberEntity.setMobile(vo.getPhone());
        memberEntity.setUsername(vo.getUserName());
        //密码进行MD5加密存储
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode(vo.getPassword());
        memberEntity.setPassword(encode);
        this.baseMapper.insert(memberEntity);
    }

    @Override
    public void checkPhoneUnique(String phone) throws PhoneExistException{
        Integer count = this.baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("mobile", phone));
        if(count>0){
            throw new PhoneExistException();
        }
    }

    @Override
    public void checkUsernameUnique(String username) throws UsernameExistException{
        Integer count = this.baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("username", username));
        if(count>0){
            throw new UsernameExistException();
        }
    }

    @Override
    public MemberEntity login(MemberLoginVo vo) {
        String loginacc = vo.getLoginacc();
        String password = vo.getPassword();
        //去数据库查询密码
        MemberEntity entity = this.baseMapper.selectOne(new QueryWrapper<MemberEntity>().eq("username", loginacc)
                .or().eq("mobile", loginacc));
        if(entity==null){
            //登录失败
            return null;
        }
        //验证密码
        String passwordDb = entity.getPassword();
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        boolean matches = passwordEncoder.matches(password, passwordDb);
        if(matches){
            return entity;
        }else {
            return null;
        }
    }

    @Override
    public MemberEntity login(GiteeUserVo vo) {
        String giteeId = vo.getId();
        MemberEntity authMember = this.baseMapper.selectOne(new QueryWrapper<MemberEntity>().eq("social_uid", giteeId));
        if(authMember != null){
            //这个用户已经注册
            return authMember;
        }else{
            MemberEntity registMember = new MemberEntity();
            registMember.setUsername(vo.getName());
            registMember.setSocialUid(vo.getId());
            this.baseMapper.insert(registMember);
            return registMember;
        }

    }

}